package com.zhxs.nfcscan.base.dialog;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;

import com.zhxs.nfcscan.R;
import com.zhhd.base.BaseDialog;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/12/2
 *    desc   : 提示对话框
 */
public final class PictureTipsDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder>
            implements Runnable, BaseDialog.OnShowListener {

        private final ImageView mIconView;

        private int mDuration = 5000;

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.picture_tips_dialog);
            setAnimStyle(BaseDialog.ANIM_TOAST);
            setBackgroundDimEnabled(false);
            setCancelable(false);
            mIconView = findViewById(R.id.iv_tips_icon);

            addOnShowListener(this);
        }

        public Builder setIcon(@DrawableRes int id) {
            mIconView.setImageResource(id);

            return this;
        }

        public Builder setImage64(String image64) {
            byte[] decodedBytes = Base64.decode(image64, Base64.DEFAULT);
            Bitmap bitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
            mIconView.setImageBitmap(bitmap);
            return this;
        }

        public Builder setDuration(int duration) {
            mDuration = duration;
            return this;
        }



        @Override
        public BaseDialog create() {
            // 如果显示的图标为空就抛出异常
            if (mIconView.getDrawable() == null) {
                throw new IllegalArgumentException("The display type must be specified");
            }
            return super.create();
        }

        @Override
        public void onShow(BaseDialog dialog) {
            // 延迟自动关闭
            postDelayed(this, mDuration);
        }

        @Override
        public void run() {
            if (!isShowing()) {
                return;
            }
            dismiss();
        }
    }
}